/**
 * 开发思想,需要灵活搭配
 */
import { GetParamJoIn } from '@/utils/baseMethods';
import axios, { AxiosResponse } from 'axios';
/**
 * 获取当前环境的请求URL
 * 处理打包时的处理
 */

class requestBody {
  requestBody;
  /**
   * @param config          基础的axios配置
   * @param requestConfig   自定义请求配置, (config)=>  config 接受参数 可打印查看
   * @param responseConfig  自定义响应拦截配置 (config)=>  config 接受参数 可打印查看
   */
  constructor(config, requestConfig?, responseConfig?) {
    this.requestBody = axios.create(config);
    /**
     * 请求拦截
     */
    this.requestBody.interceptors.request.use(
      (config) => {
        requestConfig(config);
        return config;
      },
      (error) => {
        return Promise.reject(error);
      },
    );
    /**
     * 响应总拦截
     */
    this.requestBody.interceptors.response.use(
      (response: AxiosResponse) => {
        response = responseConfig(response);
        return response.data;
      },
      function (error) {
        return Promise.reject(error);
      },
    );
  }
  /**
   * @exports APIGET 定义get请求
   * @param URL    请求的API路径
   * @param PARAMS 参数为object对象
   * @returns  object.code: number;
   *           object.msg: string;
   *           object.data: object | stirng | unkone
   */
  APIGET = (URL: string, PARAMS?: object) => this.requestBody.get(URL + GetParamJoIn(PARAMS));
  /**
   * @exports APIDELETE 定义delete请求
   * @param URL       请求的API路径
   * @param DATA      参数为object对象
   * @returns  object.code: number;
   *           object.msg: string;
   *           object.data: object | stirng | unkone
   */
  APIDELETE = (URL: string, DATA?: object) => this.requestBody.delete(URL, DATA);

  /**
   * @exports APIPOST 定义post请求
   * @param URL     请求的API的路径
   * @param PARAMS  参数为object对象
   * @returns  object.code: number;
   *           object.msg: string;
   *           object.data: object | stirng | unkone
   */
  APIPOST = (URL: string, DATA?: object) => this.requestBody.post(URL, DATA);

  /**
   * @exports APIPUT 定义put请求
   * @param URL     请求的API的路径
   * @param PARAMS  参数为object对象
   * @returns  object.code: number;
   *           object.msg: string;
   *           object.data: object | stirng | unkone
   */
  APIPUT = (URL: string, DATA?: object) => this.requestBody.put(URL, DATA);
}

export default requestBody;
